package com.rightsidetech.http.controller.admin.news;


import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.DelReq;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.news.NewsPageReq;
import com.rightsidetech.system.dtos.request.news.NewsSaveReq;
import com.rightsidetech.system.dtos.request.news.NewsUpdateReq;
import com.rightsidetech.system.dtos.response.news.NewsDetailResp;
import com.rightsidetech.system.dtos.response.news.NewsPageResp;
import com.rightsidetech.system.service.SysNewsService;
import com.rightsidetech.user.entity.UsUserLoginInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

@RestController
@RequestMapping("/admin/news/")
@ShenyuSpringMvcClient(path = "/admin/news/**")
@Api(tags = "新闻相关")
public class SysNewsController extends BaseController {

    @Resource
    private SysNewsService sysNewsService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/save")
    @ApiOperation("添加新闻")
    public ResponseData saveNews(@RequestBody NewsSaveReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return sysNewsService.saveNews(req,loginName,loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/page")
    @ApiOperation("分页新闻列表")
    public ResponseData<PageInfo<NewsPageResp>> pageNews(NewsPageReq req){
         return sysNewsService.page(req);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/detail")
    @ApiOperation("新闻详情")
    public ResponseData<NewsDetailResp> details(Long id){
         return sysNewsService.detail(id);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/update")
    @ApiOperation("更新新闻")
    public ResponseData updateNews(@RequestBody NewsUpdateReq req) throws Exception {
        ConstraintViolation validate = BeanValidator.validate(req);
        if (validate != null) {
            return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }

        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(null == loginInfoId || null == loginName){
            return ResponseData.fail("未找到登录人");
        }
        return sysNewsService.update(req, loginName, loginInfoId);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/del")
    @ApiOperation("删除新闻")
    public ResponseData delNews(@RequestBody DelReq req) throws Exception{
        Long loginInfoId = Long.parseLong(getAttribute());
        String loginName = getLoginName();
        if(StringUtils.isEmpty(loginInfoId)){
            return ResponseData.fail("未找到登录人");
        }
        return sysNewsService.del(req, loginInfoId,loginName);
    }


    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/off")
    @ApiOperation("下架新闻")
    public ResponseData offNews(@RequestBody DelReq req){
        return sysNewsService.offNews(req);
    }
}
